home *** CD-ROM | disk | FTP | other *** search
/ MacFormat España 13 / MacFormat n. 13 (Spain) / Macformat 13.bin / Shareware Internet / Desarrolladores / Extension Shell / Extension.c < prev    next >
Encoding:
C/C++ Source or Header  |  1996-01-06  |  8.2 KB  |  305 lines

  1. /*________________________________________________________
  2.  
  3.     File: Extension.c
  4.  
  5.     C code for a printing extension.
  6.  
  7.     Dave Hersey
  8.     Apple Developer Technical Support
  9.  
  10.     Modified By Dave MacLachlan
  11.     
  12.     12/01/92 - dmh - Created.
  13.      4/26/93 - dmh - Updated to use recommended approach
  14.                       to global data initialization.
  15.      9/05/93 - dmh - Updated for b2.
  16.                     - Fixed minor problem with highlighting
  17.                      of editText panel items.
  18.                    - Switched to Exception.h assertion stuff
  19.                      for error checking.
  20.     12/18/93 - dmh - Updated for b3.
  21.      3/22/94 - dmh - Updated for b4.
  22.      1/05/96 - dim - ported over to Metrowerks Codewarrior 7
  23.     (Note: all functions are in the Mark menu.)
  24.     
  25. __________________________________________________________*/
  26.  
  27. #include "Extension.h"
  28.  
  29.  
  30. asm void __Startup__()
  31. {
  32.     
  33.             DC.L    0                    ; //Reserved for owner count.
  34.  
  35.             JMP        NewInitialize        ; //(offset =  4)
  36.             JMP        NewShutDown            ; //(offset =  8)
  37.             JMP        NewJobPrintDialog    ; //(offset = 12)
  38.             JMP        NewHandlePanelEvent    ; //(offset = 16)
  39.             JMP        NewSpoolPage        ; //(offset = 20)
  40.             RTS                            ;
  41. }
  42. /*******************************************************************
  43.     InitGlobalData is used to initialize any global data we need to
  44.     in our initialize message override.  It's critical that you do
  45.     things this way, rather than access the data in the same scope
  46.     that you call NewMessageGlobals.  Otherwise, some compilers
  47.     will give you code that references an invalid A5 world.
  48.  
  49. ********************************************************************/
  50.  
  51. OSErr InitGlobalData()
  52. {
  53. // Initialize any global data here.
  54.     
  55.     return noErr;
  56. }
  57.  
  58.  
  59. /*******************************************************************
  60.     NewInitialize is our override for the GXInitialize message.  In
  61.     here, you shouldn't initialize anything directly-- call
  62.     InitGlobalData for that.  Once you create the A5 world with
  63.     NewMessageGlobals, you can access your global data just like
  64.     you were an application.  Whenever you're called, your global
  65.     data will be valid.
  66.     
  67. ********************************************************************/
  68.  
  69. OSErr NewInitialize()
  70. {
  71.     OSErr    err;
  72.  
  73. // Create an A5 world, and initialize our global data.
  74.  
  75.     err = NewMessageGlobals(A5Size(), A5Init);
  76.     
  77.     if (!err) err = InitGlobalData();
  78.     
  79.     return err;
  80. }
  81.  
  82.  
  83. /*******************************************************************
  84.     NewShutDown is our override for the GXShutDown message.  We
  85.     simply throw away our A5 world which we created in our
  86.     GXInitialize message override, NewInitialize.
  87.     
  88. ********************************************************************/
  89.  
  90. OSErr NewShutDown()
  91. {
  92.     DisposeMessageGlobals();
  93.     return noErr;
  94. }
  95.  
  96.  
  97. /*******************************************************************
  98.     NewSpoolPage is our override for the GXSpoolPage message.  We
  99.     check to see if we're enabled, and if so, give a SysBeep before
  100.     forwarding.
  101.     
  102. ********************************************************************/
  103.  
  104. OSErr NewSpoolPage(gxSpoolFile spFile, gxFormat aFormat, gxShape pgShape)
  105. {
  106.     OSErr                    err;
  107.     ExtensionCollection        extCollect;
  108.     
  109. // Try to retrieve our collection item.  If we can't find it, we'll
  110. // act as if the user had us turned off or on (as determined by
  111. // kDefaultSetting).
  112.  
  113.     err = GetJobCollectionItem(&extCollect, nil, kExtensionCollectionType,
  114.                                gxPrintingTagID);
  115.  
  116.     if (err)
  117.     {
  118.         extCollect.extTurnedOn = kDefaultSetting;
  119.         err = noErr;
  120.     }
  121.  
  122.  
  123. // If we're turned on, beep so we know we've been here, then forward
  124. // this message down the chain.
  125.  
  126.     if (extCollect.extTurnedOn)
  127.         SysBeep(3);
  128.     
  129.     err = Forward_GXSpoolPage(spFile, aFormat, pgShape);
  130.  
  131.     return err;
  132. }
  133.  
  134.  
  135. /*******************************************************************
  136.     NewJobPrintDialog is our override for GXJobPrintDialog.  All we
  137.     do is set up our panel and then forward the message.
  138.     
  139. ********************************************************************/
  140.  
  141. OSErr NewJobPrintDialog(gxDialogResult *dlogResult)
  142. {
  143.     OSErr    err;
  144.     
  145.     err = SetUpPrintPanel();
  146.  
  147.     if (!err)
  148.         err = Forward_GXJobPrintDialog(dlogResult);
  149.     
  150.     return err;
  151. }
  152.  
  153.  
  154. /*******************************************************************
  155.     NewHandlePanelEvent is our override for GXHandlePanelEvent. If
  156.     the event is one of ours, we handle it.  Otherwise, we just
  157.     forward it down the chain.
  158.     
  159. ********************************************************************/
  160.  
  161. OSErr NewHandlePanelEvent(gxPanelInfoRecord *panelInfo, gxPanelResult *panelResult)
  162. {
  163.     OSErr            err = noErr;
  164.     GrafPtr            oldPort;
  165.     DialogPtr        pDlg;
  166.     EventRecord        tempEvent;
  167.     Str255            theItem,theAction;
  168. // Get a pointer to the dialog, save our current grafPort,
  169. // and set us to the dialog's port.
  170.  
  171.     pDlg = panelInfo->pDlg;
  172.     GetPort(&oldPort);
  173.     SetPort(pDlg);
  174.     *panelResult = gxPanelNoResult;
  175.     switch (panelInfo->panelEvt)    // Handle any of these events as need be.
  176.     {
  177.         case gxPanelOpenEvt:                // Initialize and draw.
  178.             break;
  179.         case gxPanelCloseEvt:                // Your panel is going away (panel switch,
  180.                                             // confirm or cancel).
  181.             break;                            
  182.  
  183.         case gxPanelHitEvt:                    // There's a hit in your panel.
  184.             break;
  185.         case gxPanelFilterEvt:                // This is called to filter every event.
  186.             break;
  187.         case gxPanelCancelEvt:                // The user has cancelled the dialog.
  188.             break;
  189.         case gxPanelConfirmEvt:                // The user has confirmed the dialog.
  190.             break;
  191.         case gxPanelUserWillConfirmEvt:        // user has selected confirm, time to
  192.             break;                            // parse your panel interdependencies.
  193.                                         
  194.         case gxPanelDialogEvt:                // Event to be handled by dialoghandler
  195.             break;                            // (you get to see all events).
  196.  
  197.         case gxPanelOtherEvt:                // osEvts, etc.
  198.             break;
  199.  
  200. // If our panel is activating/deactivating or if the focus (which
  201. // section of the dialog is active) has changed we need to activate
  202. // our editText fields appropriately.
  203.  
  204.         case gxPanelActivateEvt:            // The dialog window has just become active.
  205.             break;
  206.         case gxPanelDeactivateEvt:            // The dialog window is becoming inactive.
  207.             break;
  208.         case gxPanelIconFocusEvt:            // The user is moving to the icon list.
  209.             break;
  210.         case gxPanelPanelFocusEvt:            // The user is moving to the panel.
  211.  
  212. /*  Here's what you would do if you had an editText field at DITL item #5:
  213.  
  214. #define d_edText    5
  215.  
  216.             if ((((DialogPeek) pDlg)->editField +1) == (panelInfo->itemCount +d_edText))
  217.             {
  218.                 if (panelInfo->panelEvt == gxPanelPanelFocusEvt)
  219.                     TEActivate(((DialogPeek) pDlg)->textH);
  220.                 else
  221.                     TEDeactivate(((DialogPeek) pDlg)->textH);
  222.             }
  223. */
  224.             break;
  225.     }
  226.  
  227. // Restore the original port as we leave.
  228.  
  229.     SetPort(oldPort);
  230.     return err;
  231. }
  232.  
  233.  
  234. /*******************************************************************
  235.     SetUpPrintPanel sets up our print panel, adding a default
  236.     ExtensionCollection item to the job collection.  This
  237.     collection item has the values we'll use to set up our panel's
  238.     controls.
  239.     
  240. ********************************************************************/
  241.  
  242. OSErr SetUpPrintPanel()
  243. {
  244.     OSErr                    err;
  245.     gxPanelSetupRecord        panelSetupRec;
  246.     ExtensionCollection        extConfig;
  247.  
  248. // Try to find our collection item.
  249.  
  250.     err = GetCollectionItem(GXGetJobCollection(GXGetJob()),
  251.                             kExtensionCollectionType,
  252.                             gxPrintingTagID,
  253.                             nil,
  254.                             &extConfig);
  255.  
  256.  
  257. // If we don't have an item in the job collection yet, store our default
  258. // settings in it.
  259.  
  260.     if (err == collectionItemNotFoundErr)
  261.     {
  262.         extConfig.extTurnedOn = kDefaultSetting;
  263.     
  264.         err = AddCollectionItem(GXGetJobCollection(GXGetJob()),
  265.                                 kExtensionCollectionType,
  266.                                 gxPrintingTagID,
  267.                                 sizeof(ExtensionCollection),
  268.                                 &extConfig);
  269.  
  270.         nrequire(err, HaveCollectionMgrError);
  271.     }
  272.  
  273.  
  274. // Now, set up the panel.
  275.  
  276.     panelSetupRec.panelResId        = r_ExtensionPanel;        // which panel resource?
  277.     panelSetupRec.resourceRefNum    = GXGetMessageHandlerResFile(); // where is it?
  278.     panelSetupRec.refCon            = 0;                       // we don't use this.
  279.     panelSetupRec.panelKind            = gxExtensionPanel;     // This is an extension panel.
  280.  
  281.     err = GXSetupDialogPanel(&panelSetupRec);
  282.  
  283.  
  284. HaveCollectionMgrError:
  285.     
  286.     return err;
  287. }
  288.  
  289.  
  290. /*******************************************************************
  291.     GetJobCollectionItem is a generic routine that retrieves a
  292.     collection item from the job collection.
  293.     
  294. ********************************************************************/
  295.  
  296. OSErr GetJobCollectionItem(void *collectItem, long *collectSize,
  297.                            OSType collectType, short collectID)
  298. {
  299.     return GetCollectionItem(GXGetJobCollection(GXGetJob()),
  300.                              collectType,
  301.                              collectID,
  302.                              collectSize,
  303.                              collectItem);
  304. }
  305.